layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 会员红包活动管理
     */
    var FlowMemberCouponActivity = {
        tableId: "flowMemberCouponActivityTable"
    };

    /**
     * 初始化表格的列
     */
    FlowMemberCouponActivity.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'couponActivityId', hide: true, title: 'ID'},
            {field: 'activityTitle', sort: true, title: '活动标题'},
            {field: 'price', sort: true, title: '金额'},
            {field: 'outDate', sort: true, title: '过期天数'},
            {field: 'onprice', sort: true, title: '使用金额'},
            {field: 'serviceName', sort: true, title: '使用服务',templet:function (row) {
                    return row.serviceType == -1 ? "无" : row.serviceName;
            }},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'activityType', sort: true, title: '活动类型',templet:function (row) {
                    return row.activityType == 0 ? "新人活动" : '平台活动';
            }},
            {align: 'center', toolbar: '#tableBar', title: '操作',width:300}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FlowMemberCouponActivity.search = function () {
        var queryData = {};

        queryData['activityTitle'] = $('#activityTitle').val();

        table.reload(FlowMemberCouponActivity.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    FlowMemberCouponActivity.openAddDlg = function () {
        func.open({
            title: '添加会员红包活动',
            content: Feng.ctxPath + '/flowMemberCouponActivity/add',
            tableId: FlowMemberCouponActivity.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      FlowMemberCouponActivity.openEditDlg = function (data) {
          func.open({
              title: '修改会员红包活动',
              content: Feng.ctxPath + '/flowMemberCouponActivity/edit?couponActivityId=' + data.couponActivityId,
              tableId: FlowMemberCouponActivity.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    FlowMemberCouponActivity.exportExcel = function () {
        var checkRows = table.checkStatus(FlowMemberCouponActivity.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FlowMemberCouponActivity.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/flowMemberCouponActivity/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(FlowMemberCouponActivity.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("couponActivityId", data.couponActivityId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FlowMemberCouponActivity.onToUserItem = function (data) {
        var operation = function () {
            var load = layui.layer.load(1);
            var ajax = new $ax(Feng.ctxPath + "/flowMemberCouponActivity/toUser", function (data) {
                Feng.success("赠送成功!");
                table.reload(FlowMemberCouponActivity.tableId);
                layui.layer.close(load);
            }, function (data) {
                Feng.error("赠送失败!" + data.responseJSON.message + "!");
                layui.layer.close(load);
            });
            ajax.async = true;
            ajax.set("couponActivityId", data.couponActivityId);
            ajax.start();
        };
        Feng.confirm("是否赠送，如已经赠送过的用户不再赠送?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FlowMemberCouponActivity.tableId,
        url: Feng.ctxPath + '/flowMemberCouponActivity/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: FlowMemberCouponActivity.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FlowMemberCouponActivity.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    FlowMemberCouponActivity.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        FlowMemberCouponActivity.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + FlowMemberCouponActivity.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            FlowMemberCouponActivity.openEditDlg(data);
        } else if (layEvent === 'delete') {
            FlowMemberCouponActivity.onDeleteItem(data);
        } else if (layEvent === 'toUser') {
            FlowMemberCouponActivity.onToUserItem(data);
        }
    });
});
